home *** CD-ROM | disk | FTP | other *** search
/ Archive Magazine CD 1995 / Archive Magazine CD 1995.iso / discs / prog_disc / volume_8 / issue_07 / std / !STDFinder / c / add next >
Encoding:
Text File  |  1993-03-08  |  1.6 KB  |  68 lines

  1. #include "stdio.h"
  2. #include "stdlib.h"
  3. #include "string.h"
  4. #include "ctype.h"
  5. #include "werr.h"
  6. #include "bbc.h"
  7.  
  8. #define mtnl   50
  9. #define mcl     8
  10.  
  11. extern struct std {
  12.   char town[mtnl];
  13.   char code[mcl];
  14.   struct std *next;
  15.   struct std *previous;
  16.   } *stdstart,*stdlast;  
  17.  
  18. extern int modified;
  19.  
  20. void add_entry(char *town,char *code)
  21. {
  22.   char *to_capitals(char *string);
  23.   struct std *add_sorted(struct std *stdnode,struct std *stdstart);
  24.   struct std *stdnode;
  25.   if ((stdnode = (struct std *) 
  26.      calloc(1, sizeof(struct std))) == NULL)
  27.     { werr(0,"Unable to assign memory for new node.\n");return;}
  28.  
  29.   strncpy(stdnode->town,to_capitals(town),mtnl-1);
  30.   strncpy(stdnode->code,code,mcl-1);
  31.  
  32.   stdstart = add_sorted(stdnode,stdstart);
  33.   modified++;/*bbc_vdu(7)*/;
  34. }
  35.  
  36. struct std *add_sorted(struct std *stdnode,struct std *stdstart)
  37. {
  38.   struct std *old,*temp;
  39.   temp = stdstart;
  40.   old = NULL;
  41.  
  42.  while (temp) { 
  43.   if (strcmp (temp->town,stdnode->town)<0) {
  44.     old = temp;  
  45.     temp = temp->next;
  46.     }
  47.   else {
  48.     if (temp->previous) {  /* then add in the middle of the list as is not NULL */
  49.       temp->previous->next = stdnode;
  50.       stdnode->next = temp;
  51.       stdnode->previous = temp->previous;
  52.       temp->previous = stdnode;
  53.       return stdstart;    /* return the original start */
  54.       }                /* insert a new first element */
  55.   stdnode->next = temp;
  56.   stdnode->previous = NULL;
  57.   temp->previous = stdnode;
  58.   return stdnode;  /* new start pointer */
  59.   }
  60.  }
  61.   old->next = stdnode; /* else add onto the end */
  62.   stdnode->next = NULL;
  63.   stdnode->previous = old;
  64.   stdlast = stdnode;
  65.   return stdstart;        /* return the original start */
  66.  
  67. }
  68.